package cn.ecnu.service.resume.impl;

import cn.ecnu.mapper.resume.*;
import cn.ecnu.pojo.resume.*;
import cn.ecnu.service.resume.ResumeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ResumeServiceImpl implements ResumeService {

    @Resource
    ResumeBasicMapper resumeBasicMapper;
    @Resource
    ResumeEduMapper resumeEduMapper;
    @Resource
    ResumeExtraMapper resumeExtraMapper;
    @Resource
    ResumeJobMapper resumeJobMapper;
    @Resource
    ResumeSkillMapper resumeSkillMapper;
    @Resource
    ResumeExpMapper resumeExpMapper;


    @Override
    public void updateOrInsertResumeBasic(ResumeBasic resumeBasic) {
        if (resumeBasicMapper.selectById(resumeBasic.getUserId()) == null){
            resumeBasicMapper.insert(resumeBasic); //如果这个用户没有简历的基本信息就插入新数据
        }else {
            resumeBasicMapper.updateById(resumeBasic); //如果有数据就更新数据
        }
    }

    @Override
    public ResumeBasic getResumeBasicByUserId(Integer userId) {
        return resumeBasicMapper.selectById(userId);
    }

    @Override
    public boolean updateResumeEdu(ResumeEdu resumeEdu) {
        return resumeEduMapper.updateById(resumeEdu) == 1;
    }

    @Override
    public boolean insertResumeEdu(ResumeEdu resumeEdu) {
        if (resumeEdu.getEduId() != null) return false;
        resumeEduMapper.insert(resumeEdu);
        return true;
    }

    @Override
    public List<ResumeEdu> getResumeEduByUserId(Integer userId) {
        LambdaQueryWrapper<ResumeEdu> wrapper = Wrappers.<ResumeEdu>lambdaQuery()
                .eq(ResumeEdu::getUserId, userId)
                .orderByDesc(ResumeEdu::getEduStart);
        return resumeEduMapper.selectList(wrapper);
    }

    @Override
    public boolean updateResumeExtra(ResumeExtra resumeExtra) {
        return resumeExtraMapper.updateById(resumeExtra) == 1;
    }

    @Override
    public boolean insertResumeExtra(ResumeExtra resumeExtra) {
        if (resumeExtra.getExtraId() != null) return false;
        resumeExtraMapper.insert(resumeExtra);
        return true;
    }

    @Override
    public List<ResumeExtra> getResumeExtraByUserId(Integer userId) {
        LambdaQueryWrapper<ResumeExtra> wrapper = Wrappers.<ResumeExtra>lambdaQuery()
                .eq(ResumeExtra::getUserId, userId);
        return resumeExtraMapper.selectList(wrapper);
    }

    @Override
    public boolean updateResumeJob(ResumeJob resumeJob) {
        return resumeJobMapper.updateById(resumeJob) == 1;
    }

    @Override
    public boolean insertResumeJob(ResumeJob resumeJob) {
        if (resumeJob.getJobId() != null) return false;
        resumeJobMapper.insert(resumeJob);
        return true;
    }

    @Override
    public List<ResumeJob> getResumeJobByUserId(Integer userId) {
        LambdaQueryWrapper<ResumeJob> wrapper = Wrappers.<ResumeJob>lambdaQuery()
                .eq(ResumeJob::getUserId, userId)
                .orderByDesc(ResumeJob::getJobStart);
        return resumeJobMapper.selectList(wrapper);
    }

    @Override
    public boolean updateResumeSkill(ResumeSkill resumeSkill) {
        return resumeSkillMapper.updateById(resumeSkill) == 1;
    }

    @Override
    public boolean insertResumeSkill(ResumeSkill resumeSkill) {
        if (resumeSkill.getSkillId() != null) return false;
        resumeSkillMapper.insert(resumeSkill);
        return true;
    }

    @Override
    public List<ResumeSkill> getResumeSkillByUserId(Integer userId) {
        LambdaQueryWrapper<ResumeSkill> wrapper = Wrappers.<ResumeSkill>lambdaQuery()
                .eq(ResumeSkill::getUserId, userId);
        return resumeSkillMapper.selectList(wrapper);
    }

    @Override
    public boolean updateResumeExp(ResumeExp resumeExp) {
        return resumeExpMapper.updateById(resumeExp) == 1;
    }

    @Override
    public boolean insertResumeExp(ResumeExp resumeExp) {
        if (resumeExp.getExpId() != null) return false;
        resumeExpMapper.insert(resumeExp);
        return true;
    }

    @Override
    public List<ResumeExp> getResumeExpByUserId(Integer userId) {
        LambdaQueryWrapper<ResumeExp> wrapper = Wrappers.<ResumeExp>lambdaQuery()
                .eq(ResumeExp::getUserId, userId);
        return resumeExpMapper.selectList(wrapper);
    }
}
